using System.Collections.Generic;
using Game.Core;
using Game.Main.Model;
using Game.Manager;
using UnityEngine;

namespace Game.Component
{
	public class SprinklerItem : BaseItem
	{
		private enum State
		{
			Close = 0,
			Open = 1
		}

		private State state;

		private List<MoundItem> mounds;

		private float timer;

		private float waitTimer;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
		}

		protected override void LateUpdate()
		{
			base.LateUpdate();
			if (state != State.Open)
			{
				return;
			}
			timer += Time.deltaTime;
			if (!(timer >= waitTimer))
			{
				return;
			}
			timer = 0f;
			ResetWaitTimer();
			if (mounds == null)
			{
				return;
			}
			int num = -1;
			for (int i = 0; i < mounds.Count; i++)
			{
				if (mounds[i].GrowUp())
				{
					num = i;
					break;
				}
			}
			if (num >= 0)
			{
				MoundItem item = mounds[num];
				mounds.RemoveAt(num);
				mounds.Add(item);
			}
		}

		private void ResetWaitTimer()
		{
			waitTimer = Random.Range(3f, 4f);
		}

		public override void QuicklyTouch(TouchEventData touchEventData)
		{
			base.QuicklyTouch(touchEventData);
			Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
			if (state == State.Close)
			{
				state = State.Open;
				timer = 0f;
				ResetWaitTimer();
				if (mounds == null)
				{
					mounds = new List<MoundItem>();
					RaycastHit2D[] array = Physics2D.BoxCastAll(base.transform.position + new Vector3(0f, 1f), new Vector2(7.22f, 1.86f), 0f, Vector2.zero, 15f, 512);
					if (array.Length > 0)
					{
						for (int i = 0; i < array.Length; i++)
						{
							RaycastHit2D raycastHit2D = array[i];
							if (raycastHit2D.collider != null && raycastHit2D.collider.transform != null && raycastHit2D.collider.transform.parent != null)
							{
								BaseItem component = raycastHit2D.collider.transform.parent.GetComponent<BaseItem>();
								if (component != null && component.itemData.scriptType == 146)
								{
									mounds.Add((MoundItem)component);
								}
							}
						}
					}
				}
				PlayDragEffect();
			}
			else
			{
				state = State.Close;
				StopDragEffect();
			}
		}
	}
}
